/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2011-6-3
 */
package com.autotag.train;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import fanglu.util.FileDealer;

public class WebServiceSelector {
	public static void main(String args[]){
		HashMap<String, WebServiceAbstract> uuidMapWSAbstract = new HashMap<String, WebServiceAbstract>();
		File file = new File("C:\\Users\\ftc\\Desktop\\ICSOC\\WSDL_FILE\\result\\result.txt");
		initServiceMap(uuidMapWSAbstract, file);
		selectRandomService(uuidMapWSAbstract, 300);
	}
	public static void selectRandomService(HashMap<String, WebServiceAbstract> uuidMapWSAbstract, 
			int size){
		ArrayList<WebServiceAbstract> aList = new ArrayList<WebServiceAbstract>();
		Iterator<String> uuid_itor = uuidMapWSAbstract.keySet().iterator();
		while(uuid_itor.hasNext()){
			String uuid = uuid_itor.next();
			aList.add(uuidMapWSAbstract.get(uuid));
		}
		Collections.sort(aList, new WebServiceAbstractRandomComparator());
		HashSet<String> sigSet = new HashSet<String>();
		int okNum = 0;
		for (int i=0; i<aList.size() && okNum < size; ++ i){
			if (sigSet.add(aList.get(i).getSig())){
				System.out.println("@" + (okNum+1));
				System.out.println( aList.get(i).getUuid().substring(0, aList.get(i).getUuid().length()-5));
				System.out.println("functionality tag:");
				System.out.println("domain tag:");
				++ okNum;
			}
		}
	}
	public static void initServiceMap(HashMap<String, WebServiceAbstract> uuidMapWSAbstract, File file){
		try {
			String fileContent = FileDealer.getFileContent(file.getAbsolutePath());
			StringReader sr = new StringReader(fileContent);
			BufferedReader br = new BufferedReader(sr);
			
			String oneLine = br.readLine();
			String thisUUID = "";
			ArrayList<String> aList = new ArrayList<String>();
			while(oneLine != null){
				if (oneLine.startsWith("#")){
					if (!thisUUID.equals("")){
						WebServiceAbstract thisA = new WebServiceAbstract(thisUUID, aList);
						uuidMapWSAbstract.put(thisUUID, thisA);
					}
					thisUUID = oneLine;
					aList = new ArrayList<String>();
				}else{
					aList.add(oneLine);
				}
				oneLine = br.readLine();
			}
			
			br.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
